package com.example.project;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.util.SparseBooleanArray;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.Toast;

import com.example.project.db.DBHelper;
import com.example.project.history.History;

public class QueryAddActivity extends Activity{
	private String[] Items;
	private ArrayAdapter<String> Adapter;
	private ListView queryselect;
	private DBHelper mDbManager;
	private int biglevel;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.addqueryactivity);
		History.ishistory = false;
		Items = getResources().getStringArray(R.array.DBMS);
		Adapter = new ArrayAdapter<String>(this, android.R.layout.
				simple_list_item_multiple_choice, Items);
		queryselect = (ListView)findViewById(R.id.queryselect);
		queryselect.setAdapter(Adapter);
		queryselect.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
		
		Spinner spin1 = (Spinner) findViewById(R.id.list1);
		spin1.setPrompt("대분류");
		
		final ArrayAdapter<CharSequence> adspin = ArrayAdapter.createFromResource(this, R.array.biglevel, android.R.layout.simple_spinner_item);
		adspin.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spin1.setAdapter(adspin);
		
		spin1.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				biglevel = position;
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {
				
			}
		});
	}
	
	public void mOnClick(View v) {
		switch (v.getId()) {
		case R.id.btnadd:
			SparseBooleanArray sb = queryselect.getCheckedItemPositions();
			int[] usingdb = new int[3];
			if (sb.size() != 0) {
				for (int i = queryselect.getCount() - 1; i >= 0 ; i--) {
					if (sb.get(i)) {
						usingdb[i] = 1;
					}else if (!sb.get(i)) {
						usingdb[i] = 0;
					}
				}
			}
			
			EditText addcategory = (EditText) findViewById(R.id.addcategory);
			EditText addquery = (EditText) findViewById(R.id.addquery);
			EditText adddesc = (EditText) findViewById(R.id.adddesc);
			
			mDbManager = DBHelper.getInstance(this);
			if(mDbManager.existQuery(addquery.getText().toString())){
				Log.d("add", "b-if");
				//이미 존재하는 쿼리
				addquery.setText("");
				Toast.makeText(this, "이미 존재하는 쿼리입니다", Toast.LENGTH_LONG).show();;
				break;
			}
			
			mDbManager.insert(addcategory.getText().toString(), 
					addquery.getText().toString(), adddesc.getText().toString(), biglevel, usingdb);
			
			queryselect.clearChoices();
			Adapter.notifyDataSetChanged();
			
			Intent intent = new Intent(this, MultiPaneWidth.class);
			intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(intent);
			break;
		}
	}
}
